let Socket = ''
let Socket2 = ''
let Socket3 = ''
let setIntervalWesocketPush = null
let setIntervalWesocketPush2 = null
let setIntervalWesocketPush3 = null

/**
 * 建立websocket连接
 * @param {string} url ws地址
 */
export const createSocket = url => {
    Socket && Socket.close()
    if (!Socket) {
        console.log('建立websocket连接')
        Socket = new WebSocket(url)
        Socket.onopen = onopenWS
        Socket.onmessage = onmessageWS
        Socket.onerror = onerrorWS
        // Socket.onclose = oncloseWS
    } else {
        console.log('websocket已连接')
    }
}
export const createSocket2 = url => {
    Socket2 && Socket2.close()
    if (!Socket2) {
        console.log('建立websocket连接2')
        Socket2 = new WebSocket(url)
        Socket2.onopen = onopenWS2
        Socket2.onmessage = onmessageWS2
        Socket2.onerror = onerrorWS2
        // Socket2.onclose = oncloseWS2
    } else {
        console.log('websocket已连接2')
    }
}
export const createSocket3 = url => {
    Socket3 && Socket3.close()
    if (!Socket3) {
        console.log('建立websocket连接3')
        Socket3 = new WebSocket(url)
        Socket3.onopen = onopenWS3
        Socket3.onmessage = onmessageWS3
        Socket3.onerror = onerrorWS3
        // Socket3.onclose = oncloseWS3
    } else {
        console.log('websocket已连接3')
    }
}


/**打开WS之后发送心跳 */
const onopenWS = () => {
    sendPing()
}
const onopenWS2 = () => {
    sendPing2()
}
const onopenWS3 = () => {
    sendPing3()
}

/**连接失败重连 */
const onerrorWS = () => {
    Socket.close()
    clearInterval(setIntervalWesocketPush)
    console.log('连接失败重连中')
    if (Socket.readyState !== 3) {
        Socket = null
        console.log('重连socket啦');
        createSocket()
    }
}
const onerrorWS2 = () => {
    Socket2.close()
    clearInterval(setIntervalWesocketPush2)
    console.log('连接失败重连中2')
    if (Socket2.readyState !== 3) {
        Socket2 = null
        console.log('重连socket2啦');
        createSocket2()
    }
}
const onerrorWS3 = () => {
    Socket3.close()
    clearInterval(setIntervalWesocketPush3)
    console.log('连接失败重连中3')
    if (Socket3.readyState !== 3) {
        Socket3 = null
        console.log('重连socket3啦');
        createSocket3()
    }
}

/**WS数据接收统一处理 */
const onmessageWS = e => {
    window.dispatchEvent(new CustomEvent('onmessageWS', {
        detail: {
            data: e.data
        }
    }))
}
const onmessageWS2 = e => {
    window.dispatchEvent(new CustomEvent('onmessageWS2', {
        detail: {
            data: e.data
        }
    }))
}
const onmessageWS3 = e => {
    window.dispatchEvent(new CustomEvent('onmessageWS3', {
        detail: {
            data: e.data
        }
    }))
}

/**
 * 发送数据但连接未建立时进行处理等待重发
 * @param {any} message 需要发送的数据
 */
const connecting = message => {
    setTimeout(() => {
        if (Socket.readyState === 0) {
            connecting(message)
        } else {
            Socket.send(JSON.stringify(message))
        }
    }, 1000)
}
const connecting2 = message => {
    setTimeout(() => {
        if (Socket2.readyState === 0) {
            connecting2(message)
        } else {
            Socket2.send(JSON.stringify(message))
        }
    }, 1000)
}
const connecting3 = message => {
    setTimeout(() => {
        if (Socket3.readyState === 0) {
            connecting3(message)
        } else {
            Socket3.send(JSON.stringify(message))
        }
    }, 1000)
}


/**
 * 发送数据
 * @param {any} message 需要发送的数据5
 */
export const sendWSPush = message => {
    if (Socket !== null && Socket.readyState === 3) {
        Socket.close()
        console.log('发送重连');
        createSocket()
    } else if (Socket.readyState === 1) {
        Socket.send(JSON.stringify(message))
    } else if (Socket.readyState === 0) {
        connecting(message)
    }
}
export const sendWSPush2 = message => {
    if (Socket2 !== null && Socket2.readyState === 3) {
        Socket2.close()
        console.log('发送重连2');
        createSocket2()
    } else if (Socket2.readyState === 1) {
        Socket2.send(JSON.stringify(message))
    } else if (Socket2.readyState === 0) {
        connecting2(message)
    }
}
export const sendWSPush3 = message => {
    if (Socket3 !== null && Socket3.readyState === 3) {
        Socket3.close()
        console.log('发送重连3');
        createSocket3()
    } else if (Socket3.readyState === 1) {
        Socket3.send(JSON.stringify(message))
    } else if (Socket3.readyState === 0) {
        connecting3(message)
    }
}




/**断开重连 */
const oncloseWS = () => {
    clearInterval(setIntervalWesocketPush)
    console.log('websocket已断开....正在尝试重连')
    if (Socket.readyState !== 2) {
        Socket = null
        createSocket()
    }
}
const oncloseWS2 = () => {
    clearInterval(setIntervalWesocketPush2)
    console.log('websocket已断开....正在尝试重连2')
    if (Socket2.readyState !== 2) {
        Socket2 = null
        createSocket2()
    }
}
const oncloseWS3 = () => {
    clearInterval(setIntervalWesocketPush3)
    console.log('websocket已断开....正在尝试重连3')
    if (Socket3.readyState !== 2) {
        Socket3 = null
        createSocket3()
    }
}
/**发送心跳
 * @param {number} time 心跳间隔毫秒 默认5000
 * @param {string} ping 心跳名称 默认字符串ping
 */
export const sendPing = (time = 5000, ping = 'ping') => {
    clearInterval(setIntervalWesocketPush)
    Socket.send(ping)
    setIntervalWesocketPush = setInterval(() => {
        Socket.send(ping)
    }, time)
}
export const sendPing2 = (time = 5000, ping = 'ping') => {
    clearInterval(setIntervalWesocketPush2)
    Socket2.send(ping)
    setIntervalWesocketPush2 = setInterval(() => {
        Socket2.send(ping)
    }, time)
}
export const sendPing3 = (time = 5000, ping = 'ping') => {
    clearInterval(setIntervalWesocketPush3)
    Socket3.send(ping)
    setIntervalWesocketPush3 = setInterval(() => {
        Socket3.send(ping)
    }, time)
}

export const closeAllSocket1 = () => {
    console.log('socket1,2,3断开连接');
    clearInterval(setIntervalWesocketPush)
    clearInterval(setIntervalWesocketPush2)
    clearInterval(setIntervalWesocketPush3)
    Socket.close()
    Socket2.close()
    Socket3.close()
    Socket = null
    Socket2 = null
    Socket3 = null
}

